<?php
/**
  * ZERO
  *
  * This is not a freeware, use is subject to license terms
  * @author ZERO DEV TEAM
  * 
  */
class PictureController extends Controller
{
	public function __construct()
	{
		$this->lang = self::getLang( array( 'Common' ) );
		parent::__construct();
		
		if ( !$this->userInfo['userId'] || !$this->userInfo['userName'] )
		{
			Url::showMsg( '对不起，您还没有登录，请先登录' , '?c=login' );
		}
	}
	
	/**
	 * 浏览
	 */
	public function run()
	{
		$pictureModel = Picture::getInstance();
		
		$db = Db::createDb();
		$db->query( "SELECT pi.pid, pi.picture_name, pi.picture_address, GROUP_CONCAT(tag_content) tag
FROM picture_info pi 
INNER JOIN pinfo_ptag_relation ptr ON ptr.pid=pi.pid
INNER JOIN picture_tag pt ON ptr.ptid=pt.ptid WHERE pi.uid = " . $this->userInfo['userId'] . " AND pi.flag = 0 GROUP BY pi.pid" );
		while( $result = $db->fetchNext() )
		{
			$picList[] = $result;
		}
		
		$userModel = User::getInstance();
		$userInfo = $userModel->getOne( array( 'uid' => $this->userInfo['userId'] ) );
		
		if ( $userInfo['picture_address'] )
		{
			$pictureInfo = $pictureModel->getOne( array( 'picture_address' => $userInfo['picture_address'] ) );
			$defaultPicId = $pictureInfo['pid'];
		}
		else
		{
			$defaultPicId = 0;
		}
		$this->viewModel->tplAssign(
			array(
				'defaultPicId',
				'picList',
			),
			array(
				$defaultPicId,
				$picList,
			)
		);
		$this->viewModel->display( 'picture_index.htm' );
	}
	
	/**
	 * 上传照片
	 */
	public function addPicture()
	{
		if ( !$this->userInfo['userId'] || !$this->userInfo['userName'] )
		{
			Url::showMsg( '对不起，您还没有登录，请先登录' , '?c=login' );
		}
		$tag = Validation::filterData( trim( $this->post['tag'] ) , 3 );
		$tagArr = explode( ' ' , $tag );

		if ( $this->post['dosubmit'] )
		{
			if ( $_FILES['picture'] && $_FILES['picture']['error'] === 0 )
			{
				$upload = new Upload();
				if ( $upload->uploadFile( $_FILES['picture'] ) )
				{
					$imagePath = $upload->attach['relative_path'];
					
					//添加图片操作
					$pictureModel = Picture::getInstance();
					$pictureData = array(
						'uid' => $this->userInfo['userId'],
						'picture_name' => $upload->attach['filename'],
						'picture_address' => $imagePath,
						'flag' => 0,
						'createdate' => date( "Y-m-d H:i:s" , SYSTIME ),
						'alterdate' => date( "Y-m-d H:i:s" , SYSTIME )
					);
					$pictureId = $pictureModel->insert( $pictureData , true );
					
					//添加标签操作
					$tagList = array();
					if ( is_array( $tagArr ) )
					{
						$tagModel = PictureTag::getInstance();
						foreach( $tagArr as $tagInfo )
						{
							if ( $dbTagInfo = $tagModel->checkIsExists( $tagInfo ) )
							{
								$tagList[] = $dbTagInfo['ptid'];
							}
							else
							{
								$tagId = $tagModel->insert( array( 'tag_content' => $tagInfo ) , true );
								$tagList[] = $tagId;
							}
						}
					}
					//处理图片和标签关系表操作
					if ( $pictureId && !empty( $tagList ) )
					{
						$picTagRelation = PictureTagRelation::getInstance();
						$picTagRelation->addPicTagRelation( $pictureId , $tagList );
					}
					
					Url::showMsg( '恭喜您，上传照片成功' , SITEURL . '/?c=picture&a=addPicture' );
				}
			}
			else
			{
				Url::showMsg( '很遗憾，上传照片失败' );
			}
		}
		
		$this->viewModel->tplAssign(
			array(
				'uploadMax',
			),
			array(
				Common::getUploadMaxSize(),
			)
		);
		$this->viewModel->display( 'add_picture.htm' );
	}
	
	/**
	 * ajax删除图片操作
	 */
	public function deletePic()
	{
		$id = ( int )$this->post['id'];
		
		if ( $id < 1 )
		{
			exit ( '1|参数错误' );
		}
		
		$pictureModel = Picture::getInstance();
		$pictureInfo = $pictureModel->getOne( array( 'pid' => $id ) );
		
		if ( !$pictureInfo )
		{
			exit( '2|此图片不存在' );
		}
		elseif ( $pictureInfo['uid'] != $this->userInfo['userId'] )
		{
			exit( '3|对不起，您没有权限设置此图片' );
		}
		
		if ( $pictureModel->update( array( 'flag' => 1 ) , array( 'pid' => $id ) ) )
		{
			$userModel = User::getInstance();
			$userInfo = $userModel->getUserInfo( $this->userInfo['userId'] );
			if ( $userInfo['picture_address'] == $pictureInfo['picture_address'] )
			{
				$userModel->update( array( 'picture_address' => '' ) , array( 'uid' => $this->userInfo['userId'] ) );
			}
			exit( '0|成功' );
		}
		else
		{
			exit( '4|删除此图片错误' );
		}
	}
}